package oct2013;

public class PowXN {
    public double pow(double x, int n) {
        double v = 1;
        boolean flag = false;
        if (n < 0) {
            n = -n;
            flag = true;
        }
        double tmp = x;
        while (n > 0) {
            if ((n & 1) != 0) {
                v *= tmp;
            }
            tmp *= tmp;
            n >>= 1;
        }
        return flag ? 1 / v : v;
    }

    public double powRecursive(double x, int n) {
        if (n == 0)
            return 1;
        double v = powRecursive(x, n >> 1);
        return v * v * n % 2 == 1 ? x : 1;
    }
}
